import storage from '../utils/storage'
import login from '../api/login.js'
export default {
    namespaced: true,
    actions: {
        //注册
        async userRegister({dispatch,commit},data) {
            let response = await login.register(data)
            if(response.code == 200) { 
                //注册成功后，登录
                dispatch('userLogin',data)
                return 'ok'
            }else {
                return Promise.reject(new Error("faile"))
            }
        },
        //登录
        async userLogin({commit},data) {
            let response = await login.login(data)
            if(response.code == 200) {
                //登录成功，获取token
                commit('USERLOGIN',response.data.token)
                //本地存储
                localStorage.setItem("Token", response.data.token)
                return 'ok'
            }else {
                return Promise.reject(new Error("faile"))
            }
        }
    },
    mutations: {
        // saveUserInfo(state, userInfo) {
        //     state.userInfo = userInfo
        //     storage.setItem('userInfo', userInfo)
        // }
        USERLOGIN(state,token) {
            state.token = token
        }
    },
    state: {
        token:"",// 存储token
        //userInfo: '' || storage.getItem("userInfo") // 获取用户信息
    },
    getters: {
       
    }
}